package offer.tree;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author DengYuan2
 * @create 2021-03-02 19:27
 */
public class n_55_2 {
    public static void main(String[] args) {
        n_55_2 n = new n_55_2();
        HashMap map = new HashMap<Integer,Integer>(4);
        map.put(1,1);
        map.put(2,2);
        map.put(3,3);
        map.put(4,4);
        new ArrayList<>();
        ArrayList<Object> list = new ArrayList<>(1);
        list.add(1);
        new ConcurrentHashMap<>();
//        TreeNode t1 = new TreeNode(5);
//        TreeNode t2 = new TreeNode(3);
//        TreeNode t3 = new TreeNode(7);
//        TreeNode t4 = new TreeNode(2);
//        TreeNode t5 = new TreeNode(4);
//        TreeNode t6 = new TreeNode(6);
//        TreeNode t7 = new TreeNode(8);
//        t1.left=t2;
////        t1.right=t3;
//        t2.left=t4;
//        t2.right=t5;
//        t3.left=t6;
//        t3.right=t7;
//        boolean res = n.IsBalanced_Solution(t1);
//        System.out.println(res);
    }

    boolean res = true;
    public boolean IsBalanced_Solution(TreeNode root) {
        TreeDepth(root);
        return res;
    }

    public int TreeDepth(TreeNode root) {
        //加n上!res，可以少走很多步骤
        if (root==null || !res){
            return 0;
        }
        int left =TreeDepth(root.left);
        int right = TreeDepth(root.right);
        if (Math.abs(left-right)>1){
            res = false;
        }
        return 1+Math.max(left,right);
    }

}
